// npm i mockjs --save
// npm i --save-dev @types/mockjs
// npm i mockjs @types/mockjs --save-dev
import Mock, {Random} from 'mockjs'

Mock.setup({
    //设置请求响应时间，6秒后再响应
    timeout: 10
})


//定义patch请求 注意不能大写
Mock.mock(RegExp('/api/pch/add'), 'patch', {
    code: 11,
    msg: 'PATCH请求数据结果',
})

Mock.mock(/api\/pch\/save/, 'patch', ({url, type, body}) => {
    console.log(url)
    console.log(type)
    console.log(body)
    let user = JSON.parse(body)
    console.log(user.id)
    console.log(user.name)
    console.log(user.age)
    return {
        code: 33,
        msg: 'PATCH数据添加成功',
    }
})


//定义put请求 注意不能写PUT大写
Mock.mock(RegExp('/api/user/add'), 'put', {
    code: 1,
    msg: 'PUT请求数据结果',
})

Mock.mock(/api\/user\/save/, 'put', ({url, type, body}) => {
    console.log(url)
    console.log(type)
    console.log(body)
    let user = JSON.parse(body)
    console.log(user.id)
    console.log(user.name)
    console.log(user.age)
    return {
        code: 22,
        msg: 'PUT数据添加成功',
    }
})


//定义post请求
Mock.mock(RegExp('/api/post/add'), 'post', {
    code: 1,
    msg: 'post请求数据结果',
})

Mock.mock(/api\/post\/save/, 'post', ({url, type, body}) => {
    console.log(url)
    console.log(type)
    console.log(body)
    let user = JSON.parse(body)
    console.log(user.id)
    console.log(user.name)
    console.log(user.age)
    return {
        code: 22,
        msg: 'post数据添加成功',
    }
})


//定义get请求 RegExp('/api/get/news')
Mock.mock(/api\/get\/news/, 'get', ({url, type, body}) => {
    console.log("------------------")
    console.log(url)
    console.log(type)
    console.log(body)

    // // 获取 GET 请求的参数 decodeURI() 函数将中文解码
    // htp://localhost/api/get/news?id=33&name=jack
    let query = decodeURI(url).split('?')[1]  //id=33&name=jack
    let params: { [propName: string]: any } = {}
    if (query) {
        query.split('&').forEach(function (pair) {
            let split = pair.split('=')
            params[split[0]] = split[1]
        })
    }

    // 输出获取到的参数
    console.log(params)
    console.log(`ID:${params.id}，姓名: ${params.name}`)

    console.log("------------------")
    console.log(Random.cname())
    return {
        status: 200,
        message: '获取新闻数据信息成功',
        color: Random.color(),
        c2: Random.rgba(),
        email: Random.email(),
        content: Random.cparagraph(10, 50)
    }
})

// Mock.js 定义规则
Mock.mock(/\/api\/users/, 'get', function (options) {
    console.log(options.url)
    console.log(options.type)
    console.log(options.body)
    // 获取请求参数
    let query = options.url.split('?')[1];
    let params: { [propName: string]: any } = {}
    if (query) {
        query.split('&').forEach(function (pair) {
            let split = pair.split('=')
            params[split[0]] = split[1]
        })
    }
    // 根据参数定义返回结果
    if (query && params.name === 'test') {
        return {
            'status': 200,
            'data': {
                'name': 'test',
                'age': 25
            }
        }
    } else {
        return {
            'status': 404,
            'message': 'User not found'
        }
    }
})

//get
// Mock.mock(RegExp('/api/info'), 'get', {
//     status: 1,
//     msg: '信息数据',
//     name: '@cname'
// })

Mock.mock(RegExp('/api/info'), 'get', ({body, url, type}) => {
        console.log(decodeURI(url))
        console.log(type)
        console.log(body)
        return {
            status: 1,
            msg: '信息数据',
            name: '@cname'
        }
    }
)

//delete 请求拦截
Mock.mock(RegExp('/api/del'), 'delete', {
    status: 200,
    msg: '删除成功'
})

Mock.mock(RegExp('/api/mydel'), 'delete', ({url, type, body}) => {
    console.log(url)
    console.log(type)
    console.log(body)
    return {
        status: 111,
        msg: '删除成功111'
    }
})

